package com.rtx.wcs.std.handle.ristowcs.rfctu;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.rtx.edi.model.api.bo.ApiMappingInfoBo;
import com.rtx.edi.platform.synctask.service.ConvertService;
import com.rtx.wcs.std.bean.po.EdiTaskDownLog;
import com.rtx.wcs.std.bean.po.RtxPalletcantilever;
import com.rtx.wcs.std.handle.ristowcs.AbsTaskDownHandle;
import com.rtx.wcs.std.service.RtxPalletcantileverService;

import cn.hutool.core.util.StrUtil;


/**
 * RF-CTU换箱
 * @author Administrator
 *
 */
@Service("RF-CTU")
public class AbsRFCTUTaskDownHandle extends AbsTaskDownHandle<RtxPalletcantilever>{
	
	Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	RtxPalletcantileverService rtxPalletcantileverService;
	
	@Autowired
	ConvertService convertService;
	
	@Override
	protected void updStatus(RtxPalletcantilever o,String response,String mappingcode) {
		
		//处理响应
		Map<String,String> resultMap = handleResponse(o,response,mappingcode);
		
		
		LambdaUpdateWrapper<RtxPalletcantilever> palletUPD = Wrappers.lambdaUpdate();
		palletUPD.eq(RtxPalletcantilever::getSerialkey, o.getSerialkey());
		palletUPD.set(RtxPalletcantilever::getEditflag, resultMap.get("editflag"));
		palletUPD.set(RtxPalletcantilever::getMessage, StrUtil.sub(resultMap.get("message"), 0, 499));
		palletUPD.set(RtxPalletcantilever::getTaskcode, resultMap.get("data"));
		rtxPalletcantileverService.updByCondition(o.getWhseid(),palletUPD);
		
		if(!"PS".equals(resultMap.get("editflag"))) {
			throw new RuntimeException("wcs返回错误-"+resultMap.get("message"));
		}
	}
	
	/**
	 * 解析返回报文
	 * @param data
	 */
	protected Map<String,String> handleResponse(RtxPalletcantilever o,String response,String mappingcode){
		Map<String,String> resultMap = new HashMap<>();
		resultMap.put("editflag", "");//PS为成功 PE为失败
		resultMap.put("message", "");
		resultMap.put("data", "");
		resultMap.put("status", "");
		
		try {
			JSONObject res = JSON.parseObject(response);
			if("0".equals(res.getString("code"))) {
				resultMap.put("editflag", "PS");
			}else {
				resultMap.put("editflag", "PE");
			}
			resultMap.put("message", res.getString("msg"));
			resultMap.put("data", o.getTaskcode());
		} catch (Exception e) {
			logger.error("",e);
			resultMap.put("message", response);
			resultMap.put("editflag", "PE");
		}finally {
			resultMap.put("data", o.getTaskcode());
		}
		return resultMap;
	};


	//{"whseid":"仓库号","lpn":"箱号"}
	@Override
	protected List<RtxPalletcantilever> queryToSendDataByRISWmsCondition(String request) {
		JSONObject data = JSON.parseObject(request);
		LambdaQueryWrapper<RtxPalletcantilever> qw = Wrappers.lambdaQuery();
		qw.eq(RtxPalletcantilever::getLpn, data.getString("lpn"));
		qw.eq(RtxPalletcantilever::getEditflag, "P");
		List<RtxPalletcantilever> list = rtxPalletcantileverService.queryByCondition(data.getString("whseid"), qw);

		list.forEach(temp->{
				if(!StringUtils.hasLength(temp.getTaskcode())) {
					temp.setTaskcode(UUID.randomUUID().toString());
				}
			}
		);
		return list;
	}


	@Override
	protected String getapiname(String request) {
		return "RF-CTU换箱";
	}


	@Override
	protected void mappingReqToLog(RtxPalletcantilever data, EdiTaskDownLog log) {
		log.setType("CTUSJ");
		log.setLpn(data.getLpn());
		log.setPositioncode(data.getPositioncode());
		log.setLoc(data.getLoc());
		log.setTaskcode(data.getTaskcode());
	}

	
	@Override
	protected JSONObject converWcsRequest(RtxPalletcantilever t, String mappingcode) {
		String source = JSON.toJSONString(t);
		String targetjson = convertService.ConvertByMappingCodeAndSource(mappingcode, source);
		return JSON.parseObject(targetjson);
	}

	@Override
	protected String getEdiRequestURL(String mappingcode) {
		ApiMappingInfoBo bo = convertService.getConfigByApimappingcode(mappingcode);
		return bo.getTargetUrl();
	}
	
	
	
}
